/*
 * Copyright (c) 2023 iQi.Co.Ltd. All rights reserved.
 */
 
using System.Collections.Generic;
using NetProtocol.POD;
using NetProtocol.Enum;
using IQIGame.Onigao.Game;
using IQIGame.Onigao.GamePlay;
using IQIGame.Onigao.Framework;
using System.Threading.Tasks;
using System;

namespace NetProtocol.Server {
	/// <summary>
	/// 商城模块
	/// module : mall
	/// </summary>
	public interface SNetMall : INetServerModule {
		Dictionary<int, MessageHandler> INetServerModule.GetModuleFunction()
        {	
            return new()
            {
                { NetMessageId.mall_buy, (session, data) =>
		            {
#if ENABLE_NET_LOG || UNITY_EDITOR
						NetProtocolHelper.ServerLog(NetMessageId.mall_buy, "Recv->" + data.ToString());
#endif
		                BuyRequest _data = (BuyRequest)data;
						return CS_buy(session, _data.mallItemCid, _data.num);
		            } 
            	},
            };
        }
        
        Dictionary<int, Func<IReceiveMessage>> INetServerModule.GetModuleRequest() 
        {
	        return new ()
	        {
	        	{NetMessageId.mall_buy, ()=> new BuyRequest()},
	        };
	    }
        
#region Server-side Interface
		/// <summary>
        /// 商城购买
        /// </summary>
	    protected BuyResult CS_buy(ISession session, int mallItemCid, int num);
#endregion
#region Client-side Interface
		/// <summary>
        /// 
        /// </summary>
        public BuyResult SC_buyResult(Task<BuyResult> future){return new BuyResult(future);}
        public BuyResult SC_buyResult(int code, List<ItemShowPOD> getItems){return new BuyResult(code, getItems);}
        public BuyResult SC_buyResult(int code){return new BuyResult(code);}
		/// <summary>
        /// 商城物品购买次数限制记录,key=mallItemCid, value=次数
        /// </summary>
		public bool SC_notifyMallRefresh(ISession session, Dictionary<int, int> mallBuyCountRecords){
	        return NetProtocolHelper.SendMessage(session, new NotifyMallRefresh(mallBuyCountRecords));
	    }
	    public void SC_notifyMallRefresh_Broadcast(List<ISession> sessions, Dictionary<int, int> mallBuyCountRecords){
	        NetProtocolHelper.BroadcastMessage(sessions, new NotifyMallRefresh(mallBuyCountRecords));
	    }
		/// <summary>
        /// 商城售卖时间更新
        /// </summary>
		public bool SC_notifyMallStartSellTime(ISession session, Dictionary<int, int> mallStartSellTime){
	        return NetProtocolHelper.SendMessage(session, new NotifyMallStartSellTime(mallStartSellTime));
	    }
	    public void SC_notifyMallStartSellTime_Broadcast(List<ISession> sessions, Dictionary<int, int> mallStartSellTime){
	        NetProtocolHelper.BroadcastMessage(sessions, new NotifyMallStartSellTime(mallStartSellTime));
	    }
		/// <summary>
        /// 充值额外奖励
        /// </summary>
		public bool SC_notifyPayInfo(ISession session, Dictionary<int, int> payCount){
	        return NetProtocolHelper.SendMessage(session, new NotifyPayInfo(payCount));
	    }
	    public void SC_notifyPayInfo_Broadcast(List<ISession> sessions, Dictionary<int, int> payCount){
	        NetProtocolHelper.BroadcastMessage(sessions, new NotifyPayInfo(payCount));
	    }
		/// <summary>
        /// 通知月卡数据
        /// </summary>
		public bool SC_notifyMonthCard(ISession session, Dictionary<int, int> monthCard){
	        return NetProtocolHelper.SendMessage(session, new NotifyMonthCard(monthCard));
	    }
	    public void SC_notifyMonthCard_Broadcast(List<ISession> sessions, Dictionary<int, int> monthCard){
	        NetProtocolHelper.BroadcastMessage(sessions, new NotifyMonthCard(monthCard));
	    }
		/// <summary>
        /// 通知充值成功
        /// </summary>
		public bool SC_notifyRecharge(ISession session, NotifyRechargePOD notifyRechargePOD){
	        return NetProtocolHelper.SendMessage(session, new NotifyRecharge(notifyRechargePOD));
	    }
	    public void SC_notifyRecharge_Broadcast(List<ISession> sessions, NotifyRechargePOD notifyRechargePOD){
	        NetProtocolHelper.BroadcastMessage(sessions, new NotifyRecharge(notifyRechargePOD));
	    }
#endregion

#region Messages
	public class BuyResult : MessageResult {
	    private List<ItemShowPOD> getItems;
	    public BuyResult(Task<BuyResult> future): base(future) { }
		public BuyResult(int code): base(code) { }
		public new BuyResult SetDelayCall(Action delayCall) { base.SetDelayCall(delayCall); return this; }
	    internal BuyResult(int code, List<ItemShowPOD> getItems){
	        this.code = code;
	        this.getItems = getItems == null ? null : new (getItems);
	    }
        public override MessageResult GetFutureResult(Task task) { return ((Task<BuyResult>)task).Result; }
		public override int GetMessageId() {return NetMessageId.mall_buyResult;}
		public override SendPackage ToPackage() {
			SendPackage si_p = NetProtocolHelper.GetSendPackage();
			si_p.Header.Messagid = NetMessageId.mall_buyResult;
			si_p.StartWrite();
			SOutputStream _out = si_p.OutputStream;
			_out.Write_int(code);
			_out.Write_list_pod(getItems);
			return si_p;
		}
		public override string ToString() {
    		return "Mall.SC_buyResult { "
			+ "code="
			+ code
			+ ", getItems="
			+ getItems.ToString<ItemShowPOD>()
			+" }";
		}
	}
	public class NotifyMallRefresh : MessageNotify {
		private Dictionary<int, int> mallBuyCountRecords;
		public NotifyMallRefresh(Dictionary<int, int> mallBuyCountRecords) {
		    this.mallBuyCountRecords = mallBuyCountRecords == null ? null : new (mallBuyCountRecords);
		}
		public override int GetMessageId() {return NetMessageId.mall_notifyMallRefresh;}
		public override SendPackage ToPackage() {
			SendPackage si_p = NetProtocolHelper.GetSendPackage();
			si_p.Header.Messagid = NetMessageId.mall_notifyMallRefresh;
			si_p.StartWrite();
			SOutputStream _out = si_p.OutputStream;
			_out.Write_map_int_int(mallBuyCountRecords);
			return si_p;
		}
		public override string ToString() {
			return "Mall.SC_notifyMallRefresh { "
			+ "mallBuyCountRecords="
			+ mallBuyCountRecords.ToString<int, int>()
			+" }";
		}
	}
	public class NotifyMallStartSellTime : MessageNotify {
		private Dictionary<int, int> mallStartSellTime;
		public NotifyMallStartSellTime(Dictionary<int, int> mallStartSellTime) {
		    this.mallStartSellTime = mallStartSellTime == null ? null : new (mallStartSellTime);
		}
		public override int GetMessageId() {return NetMessageId.mall_notifyMallStartSellTime;}
		public override SendPackage ToPackage() {
			SendPackage si_p = NetProtocolHelper.GetSendPackage();
			si_p.Header.Messagid = NetMessageId.mall_notifyMallStartSellTime;
			si_p.StartWrite();
			SOutputStream _out = si_p.OutputStream;
			_out.Write_map_int_int(mallStartSellTime);
			return si_p;
		}
		public override string ToString() {
			return "Mall.SC_notifyMallStartSellTime { "
			+ "mallStartSellTime="
			+ mallStartSellTime.ToString<int, int>()
			+" }";
		}
	}
	public class NotifyPayInfo : MessageNotify {
		private Dictionary<int, int> payCount;
		public NotifyPayInfo(Dictionary<int, int> payCount) {
		    this.payCount = payCount == null ? null : new (payCount);
		}
		public override int GetMessageId() {return NetMessageId.mall_notifyPayInfo;}
		public override SendPackage ToPackage() {
			SendPackage si_p = NetProtocolHelper.GetSendPackage();
			si_p.Header.Messagid = NetMessageId.mall_notifyPayInfo;
			si_p.StartWrite();
			SOutputStream _out = si_p.OutputStream;
			_out.Write_map_int_int(payCount);
			return si_p;
		}
		public override string ToString() {
			return "Mall.SC_notifyPayInfo { "
			+ "payCount="
			+ payCount.ToString<int, int>()
			+" }";
		}
	}
	public class NotifyMonthCard : MessageNotify {
		private Dictionary<int, int> monthCard;
		public NotifyMonthCard(Dictionary<int, int> monthCard) {
		    this.monthCard = monthCard == null ? null : new (monthCard);
		}
		public override int GetMessageId() {return NetMessageId.mall_notifyMonthCard;}
		public override SendPackage ToPackage() {
			SendPackage si_p = NetProtocolHelper.GetSendPackage();
			si_p.Header.Messagid = NetMessageId.mall_notifyMonthCard;
			si_p.StartWrite();
			SOutputStream _out = si_p.OutputStream;
			_out.Write_map_int_int(monthCard);
			return si_p;
		}
		public override string ToString() {
			return "Mall.SC_notifyMonthCard { "
			+ "monthCard="
			+ monthCard.ToString<int, int>()
			+" }";
		}
	}
	public class NotifyRecharge : MessageNotify {
		private NotifyRechargePOD notifyRechargePOD;
		public NotifyRecharge(NotifyRechargePOD notifyRechargePOD) {
		    this.notifyRechargePOD = notifyRechargePOD;
		}
		public override int GetMessageId() {return NetMessageId.mall_notifyRecharge;}
		public override SendPackage ToPackage() {
			SendPackage si_p = NetProtocolHelper.GetSendPackage();
			si_p.Header.Messagid = NetMessageId.mall_notifyRecharge;
			si_p.StartWrite();
			SOutputStream _out = si_p.OutputStream;
			_out.Write_pod(notifyRechargePOD);
			return si_p;
		}
		public override string ToString() {
			return "Mall.SC_notifyRecharge { "
			+ "notifyRechargePOD="
			+ notifyRechargePOD
			+" }";
		}
	}
	private class BuyRequest : IReceiveMessage
    {
		internal int mallItemCid;
		internal int num;
        public void FromPackage(ReceivePackage package)
		{
			SInputStream _in = package.InputStream;
            mallItemCid = _in.HasRemaining() ? _in.Read_int() : default;
            num = _in.HasRemaining() ? _in.Read_int() : default;
		}
        public int GetMessageId() { return NetMessageId.mall_buy; }
        public override string ToString()
        {
            return "Mall.buy"+ ", mallItemCid="+ mallItemCid+ ", num="+ num;
        }
    }
	}
#endregion
}